export function usePagination(root: HTMLElement, watch: HTMLElement, threshold: number = 1.0, callback: () => void) {
  let observer: IntersectionObserver | null = null;

  // 初始化 IntersectionObserver
  const initObserver = () => {
    if (!watch || !root) {
      console.log(root, watch);
      console.warn("root 或 watch 参数无效，分页功能未启用");
      return;
    }

    observer = new IntersectionObserver(
      (entries) => {
        entries.forEach((entry) => {
          if (entry.isIntersecting) {
            callback();
          }
        });
      },
      { root, threshold },
    );

    observer.observe(watch);
  };

  const destroyObserver = () => {
    if (observer && watch) {
      observer.unobserve(watch);
      observer.disconnect();
      observer = null;
    }
  };

  initObserver();

  return destroyObserver;
}
